package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.metric.MetricSourceEntity;
import com.hllxd.hism.star.basic.service.service.DataMapService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 数据地图
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/data_map")
public class DataMapController2 {

    @Autowired
    private DataMapService dataMapService;

    @Data
    public static class Param {
        public Long id;
        public String ids;
        public String topicTbl;
        private String phoneNumber;

        public String dt;
        public String dt2;

        public String topic;
        public String search;
    }

    @RestGrpcMapping("/listMetricTopic.do")
    public NTSTATUS listMetricTopic(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            // 请求主题列表（即主题导航栏信息）
            List<MetricSourceEntity> items = dataMapService.listMetricTopic();
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/listMetricSource.do")
    public NTSTATUS listMetricSource(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            // 请求主题的表信息
            List<MetricSourceEntity> items = dataMapService.listMetricSource(aa.topic);
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    @RestGrpcMapping("/searchMetricSource.do")
    public NTSTATUS searchMetricSource(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            // 搜索表信息
            List<MetricSourceEntity> items = dataMapService.searchMetricSource(aa.search);
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getMetricSource.do")
    public NTSTATUS getMetricSource(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            // 查看表信息
            MetricSourceEntity item;
            if(aa.id!=null) {
                item = dataMapService.getMetricSource(aa.id);
            }else {
                item = dataMapService.getMetricSourceByTbl(aa.topicTbl);
            }
            result.setData(item);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    @RestGrpcMapping("/setMetricSource.do")
    public NTSTATUS setMetricSource(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            MetricSourceEntity aa = params.driverObject(MetricSourceEntity.class);
            // 修改表信息
            Object res = dataMapService.setMetricSource(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/deleteMetricSource.do")
    public NTSTATUS deleteMetricSource(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            // 删除表信息
            Boolean ret = dataMapService.deleteMetricSource(aa.ids);
            result.setData(ret);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

//    @RestGrpcMapping("/syncDpMeta.do")
//    public NTSTATUS syncDpMeta(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            Param aa = params.driverObject(Param.class);
//            // 查看表信息
//            MetricSourceEntity item = dataMapService.syncDpMeta(aa.topicTbl);
//            result.setData(item);
//            return NTSTATUS.SUCCESS_STATUS();
//        }catch (BusinessException e) {
//            log.warn("Exception: {}. {}",e.toString(), params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        }catch (Exception e) {
//            log.error("Exception: {}. {}", e.toString(), params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }


}
